﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Graphics;

using Microsoft.Xna.Framework;

using ROMAP.Utilities;

namespace ROMAP.Entities.Tiles
{
    public class PulsedTileControl
    {
        private const int MAX_NUM_TIER = 4;
        private const double EFFECT_DELAY = .1;

        private List<Vector3> _affectedTiles;          // The list of tile positions that have the effect as well as the tier
        private Vector2i TilePosition;

        protected string filename;

        #region Constructor
        public PulsedTileControl(int x, int y)
        {
            TilePosition = new Vector2i(x, y);
            Initialize();
        }
        #endregion

        #region Properties
        // The last time the tier of the effect changed
        public double LastTierChange
        {
            get;
            set;
        }

        //The current tier the pulse effect is at
        public int Tier
        {
            get;
            set;
        }

        public List<Vector3> AffectedTiles
        {
            get { return _affectedTiles; }
        }

        public int MaxTiers
        {
            get { return MAX_NUM_TIER; }
        }

        public double EffectDelay
        {
            get { return EFFECT_DELAY; }
        }
        #endregion

        protected void Initialize()
        {
            Vector3 firstTile;
            firstTile.X = TilePosition.X;
            firstTile.Y = TilePosition.Y;
            firstTile.Z = 1;

            _affectedTiles = new List<Vector3>();
            Tier = 1;
            _affectedTiles.Add(firstTile);
            GetTileList();

            LastTierChange = TimeManager.CurrentTime;
        }

        public void Destroy()
        {

        }

        public void Activity()
        {
            throw new NotImplementedException();
        }

        public void Add(Tile t)
        {
            //if (!_affectedTiles.Contains(t))
            //    _affectedTiles.Add(t);
        }

        public void GetTileList()
        {
            for (int i = 1; i < MAX_NUM_TIER; i++)
            {
                List<Vector3> addedTiles = new List<Vector3>();

                foreach (Vector3 t in _affectedTiles)
                {
                    if (i == t.Z)
                    {
                        Vector3 up = new Vector3();
                        Vector3 down = new Vector3();
                        Vector3 left = new Vector3();
                        Vector3 right = new Vector3();

                        up.X = t.X; up.Y = t.Y + 1;
                        down.X = t.X; down.Y = t.Y - 1;
                        left.X = t.X - 1; left.Y = t.Y;
                        right.X = t.X + 1; right.Y = t.Y;

                        up.Z = i + 1; down.Z = i + 1;
                        left.Z = i + 1; right.Z = i + 1;

                        if (!ListContains(up))
                            addedTiles.Add(up);
                        if (!ListContains(down))
                            addedTiles.Add(down);
                        if (!ListContains(left))
                            addedTiles.Add(left);
                        if (!ListContains(right))
                            addedTiles.Add(right);
                    }
                }

                _affectedTiles.AddRange(addedTiles);
            }
        }

        private bool ListContains(Vector3 v)
        {
            foreach (Vector3 t in _affectedTiles)
            {
                if ((int)t.X == (int)v.X && (int)t.Y == (int)v.Y)
                    return true;
            }
            return false;
        }

    }
}
